<template>
    <div class="xlsxtable">
        <el-button :type="type?type:'danger'" :size="size" :icon="loading?'el-icon-loading':'el-icon-upload2'" :disabled="loading">{{title}}</el-button>
        <input type="file" @change="importf(this)" />
    </div>
</template>
<script>
const ERR_OK = 200;
export default {
    props: {
        size: String,
        title: String,
        type: String
    },
    data() {
        return {
            loading: false,
            excelData: []
        }; 
    },
    methods: {
        importf(obj) {
            let _this = this;
            let inputDOM = this.$refs.inputer;
            
            // 通过DOM取文件数据
            this.file = event.currentTarget.files[0];
            console.log(event)
            var filetypes = [
                "application/vnd.ms-excel",
                "text/xml, application/xml",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "application/wps-office.xlsx",
                ".csv"
            ];
            if (this.file) {
                if (filetypes.indexOf(this.file.type) === -1) {
                    this.$message.error("请上传.xlsx格式");
                    return;
                }
                _this.loading = true
                var rABS = false; //是否将文件读取为二进制字符串
                var f = this.file;
                var reader = new FileReader();
                FileReader.prototype.readAsBinaryString = function(f) {
                    var binary = "";
                    var rABS = false; //是否将文件读取为二进制字符串
                    var pt = this;
                    var wb; //读取完成的数据
                    var outdata;
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var bytes = new Uint8Array(reader.result);
                        var length = bytes.byteLength;
                        for (var i = 0; i < length; i++) {
                            binary += String.fromCharCode(bytes[i]);
                        }
                        var XLSX = require("xlsx");
                        if (rABS) {
                            wb = XLSX.read(btoa(fixdata(binary)), {
                                type: "base64"
                            });
                        } else {
                            wb = XLSX.read(binary, {
                                type: "binary"
                            });
                        }
                        outdata = XLSX.utils.sheet_to_json(
                            wb.Sheets[wb.SheetNames[0]]
                        );
                        _this.$emit("handleSelectedFile", outdata);
                    };
                    reader.readAsArrayBuffer(f);
                };
                if (rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            }
        },
        success(){
            this.loading = false
            this.$message.success("上传成功");
        }
    }
};
</script>
<style lang="stylus" scoped>
.xlsxtable {
    position: relative;
    overflow: hidden;
    display: inline-block;
    vertical-align: middle;

    input {
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
        cursor: pointer;
    }
}
</style>

